import { getToken } from '@/utils/auth' // 验权
import NProgress from 'nprogress' // Progress 进度条
import 'nprogress/nprogress.css' // Progress 进度条样式
import router from './router'
import store from './store'

const whiteList = ['/login','/home'] // 不重定向白名单
router.beforeEach((to, from, next) => {
  NProgress.start()
  if (getToken()) {
    if (to.path === '/login') {
      console.log('111')
      next({ path: '/' })
      NProgress.done() // if current page is dashboard will not trigger	afterEach hook, so manually handle it
    } else {
      if (store.getters.roles.length === 0) {

          let menus=[{"hidden":0,"level":0,"icon":"product","sort":0,"title":"商品","parentId":0,"createTime":1580626236000,"name":"pms","id":1},
            {"hidden":0,"level":1,"icon":"product-list","sort":0,"title":"商品列表","parentId":1,"createTime":1580626310000,"name":"product","id":2},
            {"hidden":0,"level":1,"icon":"product-add","sort":0,"title":"添加商品","parentId":1,"createTime":1580626364000,"name":"addProduct","id":3},
            {"hidden":0,"level":1,"icon":"product-cate","sort":0,"title":"商品分类","parentId":1,"createTime":1580626431000,"name":"productCate","id":4},
            {"hidden":0,"level":1,"icon":"product-attr","sort":0,"title":"商品类型","parentId":1,"createTime":1580626491000,"name":"productAttr","id":5},
            {"hidden":0,"level":1,"icon":"product-brand","sort":0,"title":"品牌管理","parentId":1,"createTime":1580626589000,"name":"brand","id":6},
            {"hidden":0,"level":0,"icon":"order","sort":0,"title":"订单","parentId":0,"createTime":1580633647000,"name":"oms","id":7},
            {"hidden":0,"level":1,"icon":"product-list","sort":0,"title":"订单列表","parentId":7,"createTime":1580633718000,"name":"order","id":8},
            {"hidden":0,"level":1,"icon":"order-setting","sort":0,"title":"订单设置","parentId":7,"createTime":1580633806000,"name":"orderSetting","id":9},
            {"hidden":0,"level":1,"icon":"order-return","sort":0,"title":"退货申请处理","parentId":7,"createTime":1580633859000,"name":"returnApply","id":10},
            {"hidden":0,"level":1,"icon":"order-return-reason","sort":0,"title":"退货原因设置","parentId":7,"createTime":1580633980000,"name":"returnReason","id":11},
            {"hidden":0,"level":0,"icon":"sms","sort":0,"title":"营销","parentId":0,"createTime":1580804280000,"name":"sms","id":12},
            {"hidden":0,"level":1,"icon":"sms-flash","sort":0,"title":"秒杀活动列表","parentId":12,"createTime":1580804362000,"name":"flash","id":13},
            {"hidden":0,"level":1,"icon":"sms-coupon","sort":0,"title":"优惠券列表","parentId":12,"createTime":1580804416000,"name":"coupon","id":14},
            {"hidden":0,"level":1,"icon":"product-brand","sort":0,"title":"品牌推荐","parentId":12,"createTime":1581063758000,"name":"homeBrand","id":16},
            {"hidden":0,"level":1,"icon":"sms-new","sort":0,"title":"新品推荐","parentId":12,"createTime":1581063794000,"name":"homeNew","id":17},
            {"hidden":0,"level":1,"icon":"sms-hot","sort":0,"title":"人气推荐","parentId":12,"createTime":1581063998000,"name":"homeHot","id":18},
            {"hidden":0,"level":1,"icon":"sms-subject","sort":0,"title":"专题推荐","parentId":12,"createTime":1581064096000,"name":"homeSubject","id":19},
            {"hidden":0,"level":1,"icon":"sms-ad","sort":0,"title":"广告列表","parentId":12,"createTime":1581064122000,"name":"homeAdvertise","id":20},
            {"hidden":0,"level":0,"icon":"ums","sort":0,"title":"权限","parentId":0,"createTime":1581064153000,"name":"ums","id":21},
            {"hidden":0,"level":1,"icon":"ums-admin","sort":0,"title":"用户列表","parentId":21,"createTime":1581064191000,"name":"admin","id":22},
            {"hidden":0,"level":1,"icon":"ums-role","sort":0,"title":"角色列表","parentId":21,"createTime":1581064213000,"name":"role","id":23},
            {"hidden":0,"level":1,"icon":"ums-menu","sort":0,"title":"菜单列表","parentId":21,"createTime":1581064253000,"name":"menu","id":24},
            {"hidden":0,"level":1,"icon":"ums-resource","sort":0,"title":"资源列表","parentId":21,"createTime":1581064273000,"name":"resource","id":25}];

          let username='admin';
          store.dispatch('GetInfo');
          console.log('222');
          store.dispatch('GenerateRoutes', { menus,username }).then(() => { // 生成可访问的路由表
            router.addRoutes(store.getters.addRouters); // 动态添加可访问路由表
            next({ ...to, replace: true })
          })

        // store.dispatch('GetInfo').then(res => { // 拉取用户信息
        //   let menus=res.data.menus;
        //   let username=res.data.username;
        //   store.dispatch('GenerateRoutes', { menus,username }).then(() => { // 生成可访问的路由表
        //     router.addRoutes(store.getters.addRouters); // 动态添加可访问路由表
        //     next({ ...to, replace: true })
        //   })
        // }).catch((err) => {
        //   store.dispatch('FedLogOut').then(() => {
        //     Message.error(err || 'Verification failed, please login again')
        //     next({ path: '/' })
        //   })
        // })
      } else {
        next()
      }
    }
  } else {
    if (whiteList.indexOf(to.path) !== -1) {
      next()
    } else {
      next('/login')
      NProgress.done()
    }
  }
})

router.afterEach(() => {
  NProgress.done() // 结束Progress
})
